#include "programmanagement.hpp"

ProgramManagement::ProgramManagement(QObject *parent) :
    QObject(parent)
{
}


ProgramManagement::ProgramManagement(ProgramManagement &other)
{
    for(int i = 0; i < other.getCommandList()->size(); i++)
    {
	Command temp = Command(other.getCommandList()->at(i));
	this->commandList.append(temp);
    }

    QList<int> keys = other.getCommandMap()->keys();
    for(int i = 0; i < keys.size(); i++)
    {
	Command temp = Command(Command(other.getCommandMap()->value(keys.at(i))));
	this->commandMap.insert(keys.at(i), temp);
    }
}


ProgramManagement::~ProgramManagement()
{
}


void ProgramManagement::addCommand(Command &command)
{
    if (command.getAddress() >= 0)
	commandMap.insert(command.getAddress(), command);
    commandList.append(command);
}


const QList<Command> *ProgramManagement::getCommandList() const
{
    return &commandList;
}


QMap<int, Command> *ProgramManagement::getCommandMap()
{
    return &commandMap;
}


void ProgramManagement::reset()
{
    this->commandMap.clear();
    this->breakpointList.clear();

    while (!this->commandList.isEmpty())
	this->commandList.takeFirst();
}


void ProgramManagement::onRequestBreakpoint(int line)
{
    //Check if any Command is there
    if (commandList.isEmpty())
	return;

    //Check if the line is a Command
    Command current = this->commandList.at(line - 1);
    if (current.getCommandType() != Command::COMMAND)
	return;

    //Toggle the breakpoint
    if (!this->breakpointList.contains(current.getAddress()))
	this->breakpointList.append(current.getAddress());
    else
	this->breakpointList.removeOne(current.getAddress());

    emit breakPointToggled(line, this->breakpointList.contains(current.getAddress()), current.getAssembler(), current.getAnchor());
}


bool ProgramManagement::hasBreakpointOnLine(int line) const
{
    return this->breakpointList.contains(line);
}
